﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MusicOnLine.Models;
using System.Text;
using Microsoft.JScript;
using MusicOnLine.Common;


namespace MusicOnLine.Controllers
{
    public class AACInfoController : Controller
    {
        MusicPlayEntities musicDB = new MusicPlayEntities();


        public ActionResult AlbumSongs(int id)
        {
            var model = musicDB.AACInfoes.Where(o => o.AlbumID == id).ToList();
            return PartialView("~/Views/CustControl/AlbumSongs.cshtml", model);
        }

        public ViewResult Index()
        {
            return View(musicDB.AACInfoes.Take(20));            
        }
        [OutputCache(Duration=0)]
        public ActionResult Play()
        {           
            
            return View();
        }
        
        public string Collection(int ID)
        {
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                UserAAC u = new UserAAC();
                u.AACID = ID;
                u.UserName = HttpContext.User.Identity.Name;
                u.AddDate = DateTime.Now;
                int count = musicDB.UserAACs.Where(o => o.AACID == ID && o.UserName == u.UserName).Count();
                if (count > 0)
                {
                    return "exsist";
                }
                musicDB.UserAACs.Add(u);
                musicDB.SaveChanges();
                return "true";
            }
            else
            {
                return "false";
            }
            
        }

        protected override void Dispose(bool disposing)
        {
            musicDB.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult SingerAlbums(int ID)
        {
            SingerAlbum model = new SingerAlbum();
            model.Singer = musicDB.Artists.Single(o => o.ID == ID);
            model.TopSongs = musicDB.AACInfoes.Where(o => o.ArtistID == ID).OrderByDescending(o => o.PlayedCount).Take(10).ToList();
            model.SingerAlbums = musicDB.Albums.Where(o => o.ArtistID == ID).ToList();
            return View(model);
        }

        public ActionResult AlbumList()
        {
            return View();
        }

        public ActionResult QuickSearchAlubm(string term)
        {
            var model = musicDB.Albums.Where(o => o.AlbumName.Contains(term) || o.ArtistName.Contains(term)).Select(o => new { value=o.AlbumName}).Take(10).ToList();
            return Json(model, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AlbumJson(string g,int PageIndex)
        {
            string s = HttpUtility.UrlDecode(g, System.Text.Encoding.GetEncoding("utf-8"));
            PageList<Album> mode = new PageList<Album>(musicDB.Albums.Where(o => o.AlbumName.Contains(s) || o.ArtistName.Contains(s)).OrderByDescending(o=>o.CreateDate), PageIndex, 10);
            return Json(mode, JsonRequestBehavior.AllowGet);
        }
    }
}